<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/2/20
 * Time: 21:21
 */
class Class_plan_model extends CI_Model
{

  /**
   * 获取今日上课
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_today_class($date,$school_id)
  {
    if(empty($date)){
      $date = date("Y-m-d");
    }

    $sql = "select plan.class_id,plan.class_date,plan.class_time,plan.class_hour,tea.teacher_name,cla.campus,campus.name as campus_name,room.name as class_room,plan.class_type,plan.class_intro,plan.remark,cla.class_name,cla.class_num,cla.course_name from edu_o2o_class_plan plan
      left join edu_o2o_class cla on cla.id=plan.class_id
      left join edu_o2o_school_campus campus on campus.id=cla.campus
      left join edu_o2o_class_teacher tea on tea.id=plan.teacher_id
      left join edu_o2o_classroom room on room.id=plan.class_room
      where plan.class_date = '{$date}' and plan.school_id = {$school_id}";
    // $sql = "select * from {$this->db->dbprefix}o2o_class_plan where class_date = '{$today}' and school_id = {$school_id}";
    return $this->db->query($sql)->result_array();
  }

  /**
   * 单个添加计划
   * @param [type] $data     [description]
   * @param [type] $class_id [description]
   */
  public function add_single($data,$class_id)
  {
    //查询class
    $sql = "select * from {$this->db->dbprefix}o2o_class where id = {$class_id} limit 1";
    $class = $this->db->query($sql)->row_array();
    if(empty($class)) return -1;


    // print_r($data['class_hour']);exit;
    $data['class_date'] = $data['class_time'];
    $data['class_id'] = $class_id;
    $data['school_id'] = $class['school_id'];
    $data['count_chuqin'] = 0;
    $data['count_kuangke'] = 0;
    $data['count_chidao'] = 0;
    $data['count_zaotui'] = 0;
    $data['count_qingjia'] = 0;
    $data['status'] = 0;
    $data['add_time'] = time();
    return $this->db->insert('o2o_class_plan',$data);
  }

  /**
   * 自动排课
   * @param  [type] $data     [description]
   * @param  [type] $class_id [description]
   * @return [type]           [description]
   */
  public function auto_plan($data,$class_id)
  {

    //查询class
    $sql = "select * from {$this->db->dbprefix}o2o_class where id = {$class_id} limit 1";
    $class = $this->db->query($sql)->row_array();
    if(empty($class)) return -1;

    //查询需要排多少课时
    $hour_count_result = $this->db->query("SELECT class_hour-(select count(0) from {$this->db->dbprefix}o2o_class_plan where class_id ={$class_id}) as class_hour from {$this->db->dbprefix}o2o_class where id = {$class_id}")->row_array();
    $hour_count = $hour_count_result['class_hour'];

    //排课日期list
    $data_list = array();

    $start_time = $data['class_time'];//开始排课时间
    //$start_week = $this->get_week($start_time);//开始时间是星期几

    $i = 0;
    /***开始排课***/
    while ( $hour_count > 0) {
      //1开始$data['args']
      foreach ($data['args'] as $args) {

        $arg_json = json_decode($args,true);



        if($hour_count>=$arg_json['hour_time']){
          // print_r($arg_json['week']);exit;
          $next_data = $this->get_data_by_week($arg_json['week'],$start_time);

          $data_list[$i]=$next_data;
          //从第二天开始

          $start_time =  date("Y-m-d",strtotime("$next_data   + 1 day"));
          $i++;
          //插入排课数据
          // $class_plan = array(
          //   'title' => '',
          //   'class_id' => $class_id,
          //   'school_id' => $class['school_id'],
          //   'class_date' => $next_data,
          //   'class_time' => $next_data.' '.$arg_json['start_time'],
          //   'class_time_str' => $arg_json['start_time'] .'-'.$arg_json['end_time'] ,
          //   'class_hour' => $arg_json['hour_time'],//课时
          //   'teacher_id' => $data['teacher_id'],
          //   'class_room' => $data['class_room'],
          //   'class_type' => 0,
          //   'count_chuqin' => 0,
          //   'count_kuangke' => 0,
          //   'count_chidao' => 0,
          //   'count_zaotui' => 0,
          //   'count_qingjia' => 0,
          //   'status' => 0,
          //   'add_time' => time(),
          //   'add_by' => '',
          // );
          // $this->db->insert('o2o_class_plan',$class_plan);

          $hour_count = $hour_count - $arg_json['hour_time'];//剩余课时-本次课时
        }


      }
    }
    // print_r($data_list);exit;
    // for ($i=0; $i < $hour_count; $i++) {
    //   //1开始$data['args']
    //   foreach ($args as $data['args']) {
    //     $arg_json = json_encode($args);
    //     print_r($arg_json);exit;
    //     $next_data = $this->get_data_by_week($arg_json['week']);
    //
    //   }
    // }



    /***结束排课***/



    //开始添加数据
    $hour = $data['hour'];
    $minute = $data['minute'];
    $class_hour = $arg_json['hour_time'];
    $teacher_id = $data['teacher_id'];
    $class_room = $data['class_room'];


    foreach ($data_list as $class_date) {
      print_r($class_date);
      $class_plan = array(
        'title' => '',
        'class_id' => $class_id,
        'school_id' => $class['school_id'],
        'class_date' => $class_date,
        'class_time' => $class_date.' '.$hour.':'.$minute,
        'class_hour' => $class_hour,
        'teacher_id' => $teacher_id,
        'class_room' => $class_room,
        'class_type' => 0,
        'count_chuqin' => 0,
        'count_kuangke' => 0,
        'count_chidao' => 0,
        'count_zaotui' => 0,
        'count_qingjia' => 0,
        'status' => 0,
        'add_time' => time(),
        'add_by' => '',
      );
      $this->db->insert('o2o_class_plan',$class_plan);
    }
    return 1;
    // return $next_data;



  }


  function get_data_by_week($week,$start_time){
    //下一个开始排课日期
      $next_data;
      // $start_time = $data['class_time'];//开始排课时间
      $start_week = $this->get_week($start_time);//开始时间是星期几

    //选择排课周期（星期几）
    if($week<7){
      //不是全周


      //获取下一个week的日期
      if($start_week == $week){
        //就是今天
        $next_data = $start_time;
      }else{
         $next_week = $week - $start_week;
         if($next_week<0) $next_week+=7;
         $next_data =  date("Y-m-d",strtotime("$start_time   + $next_week  day"));
      }

      // //排课日期list计算
      // $data_list[] = $next_data;

      // for ($i=1; $i < $hour_count; $i++) {
      //   $next_data = date("Y-m-d",strtotime("$next_data   + 7  day"));
      //   $data_list[] = $next_data;
      // }

    }else{
      //全周，从今天开始
      $next_data = $start_time;
      //排课日期list计算
      // $data_list[] = $next_data;

      // for ($i=1; $i < $hour_count; $i++) {
      //   $next_data = date("Y-m-d",strtotime("$next_data   + 1  day"));
      //   $data_list[] = $next_data;
      // }
    }

    return $next_data;
  }


  //获取星期方法
  function get_week($date){
    //强制转换日期格式
    $date_str=date('Y-m-d',strtotime($date));
    //封装成数组
    $arr=explode("-", $date_str);
    //参数赋值
    //年
    $year=$arr[0];
    //月，输出2位整型，不够2位右对齐
    $month=sprintf('%02d',$arr[1]);
    //日，输出2位整型，不够2位右对齐
    $day=sprintf('%02d',$arr[2]);
    //时分秒默认赋值为0；
    $hour = $minute = $second = 0;
    //转换成时间戳
    $strap = mktime($hour,$minute,$second,$month,$day,$year);
    //获取数字型星期几
    $number_wk=date("w",$strap);
    //自定义星期数组
    $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    //获取数字对应的星期
    return $number_wk;
  }


  /**
   * 获取班级上课计划
   * @param  [type] $class_id [description]
   * @return [type]           [description]
   */
  public function get_plan_by_class($class_id)
  {
    // $sql = "select cp.* from {$this->db->dbprefix}o2o_class_plan cp left join where cp.class_id=$class_id order by cp.class_time asc  ";
    $sql = "select cp.*,ot.teacher_name,oc.name as class_room_name from {$this->db->dbprefix}o2o_class_plan cp
            left join {$this->db->dbprefix}o2o_teachers ot on ot.id = cp.teacher_id
            left join {$this->db->dbprefix}o2o_classroom oc on oc.id = cp.`class_room`
            where cp.class_id=$class_id order by cp.class_time asc ";
    return $this->db->query($sql)->result_array();
  }

  function get_plan_by_id($id){
    $sql = "select * from {$this->db->dbprefix}o2o_class_plan where id = $id limit 1";
    return $this->db->query($sql)->row_array();
  }

  function delete_plan($id){
    return $this->db->delete('o2o_class_plan', array('id' => $id ));
  }
}
